#include "Receiver.h"
#include <iostream>
#include <sstream>
Receiver::Receiver(tcp::socket* socket, int ID)
{
    mSession = new Session(socket, ID);
    //ctor
}

Receiver::Receiver(udp::socket* socket, int ID)
{
    mSession = new Session(socket, ID);
    //ctor
}

Receiver::~Receiver()
{
    //dtor
}
void Receiver::Receive()
{
    //char protocol[2] = {'\r','\n'};
    //boost::asio::async_read_until(mSession->mSocket, mSession->mStream,"",
    //                            boost::bind(&Receiver::CreateBuffer, this, boost::asio::placeholders::error));
    //boost::array<char, 32768> buffer_;
    try
    {
        if(mTCPSocket)
        {
            mSession->mTCPSocket->async_read_some(boost::asio::buffer(buffer_),
                                          boost::bind(&Receiver::CreateBuffer, this, boost::asio::placeholders::error,
                                                      boost::asio::placeholders::bytes_transferred));
        }
        else if(mUDPSocket)
        {

        }
    }
    catch (std::logic_error const& le)
    {
        std::cerr << "STD::LOGIC_ERROR: " << le.what() << std::endl;
    }
    catch (boost::exception &e)
    {
        std::cerr << "Receiver::Receive::BOOST_ERROR: " << ((std::string)boost::diagnostic_information(e)).c_str() << std::endl;
    }
    catch (...)
    {
        std::cout<<"UNKNOWN_ERROR"<<std::endl;
    }

}
void Receiver::CreateBuffer( const boost::system::error_code& error,std::size_t bytes_transferred)
{
    try
    {
        mSession->errorCode = error;
        if (error!=boost::asio::error::eof)
        {

            /*std::istream stream(&mSession->mStream);
             //std::string str((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
             std::stringstream oss;
             oss << stream.rdbuf();

             //std::getline(stream, str);*/
            while (mSession->isWrittingBuffer)
                Util::Delay(0);
            mSession->isWrittingBuffer = true;

            mSession->mBuffer = buffer_;
            mSession->bytes_transferred = bytes_transferred;


            mSession->isWrittingBuffer = false;
            NotifyListeners();
            Receive();
        }
        else
        {
            //throw std::logic_error("ERRO AQUI NO CREATEBUFFER");
            std::list<EventListener*>::iterator iter;
            for (iter=mListeners->begin();iter!=mListeners->end();iter++)
            {
                EventListener *evt = (*iter);
                evt->OnDisconnet(mSession);
            }
        }
    }
    catch (std::logic_error const& le)
    {
        std::cerr << "STD::LOGIC_ERROR: " << le.what() << std::endl;
    }
    catch (boost::exception &e)
    {
        std::cerr << "Receiver::CreateBuffer::BOOST_ERROR: " << ((std::string)boost::diagnostic_information(e)).c_str() << std::endl;
    }
    catch (...)
    {
        std::cout<<"UNKNOWN_ERROR"<<std::endl;
    }
}

void Receiver::AddListener(EventListener* listener)
{
    mListeners->push_back(listener);
}

void Receiver::SetListener(std::list<EventListener*> *listeners)
{
    mListeners = listeners;
}

void Receiver::NotifyListeners()
{
    std::list<EventListener*>::iterator iter;
    for (iter=mListeners->begin();iter!=mListeners->end();iter++)
    {
        EventListener *evt = (*iter);
        evt->OnReceive(mSession);
    }
}
